package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerReportProgressService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName:CerReportProgressController
 * @功能：上报进度信息
 **/
@RestController
@Slf4j
@RequestMapping("/v1/reportProgress/")
public class CerReportProgressController extends BaseController {

    @Autowired
    private CerReportProgressService cerReportProgressService;

    @PostMapping("listPageData")
    @ApiOperation("获取所有的上报进度信息列表")
    public JsonObj listPageData() {
        return cerReportProgressService.listPageData(getPageData());
    }

    @PostMapping("exportData")
    @ApiOperation("点击导出按钮，获取所有的上报进度信息列表")
    public void exportData(HttpServletResponse response) {
        PageData pageData = getPageData();

        // 查询出可以用来导出的数据源
        JsonObj jsonObj = cerReportProgressService.queryAll(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
            .getData()
            .get("data");

        Object type = jsonObj
            .getData()
            .get("type");

        if (Objects.nonNull(type)) {
            List<String> headList;
            if (String
                .valueOf(type)
                .equals("1")) {
                headList = Arrays.asList("城市",
                                         "区县",
                                         "上报机构",
                                         "胃镜总量",
                                         "初始数据库临时库-应报数",
                                         "初始数据库临时库-上报进度%",
                                         "初始数据库临时库-合格",
                                         "初始数据库临时库-不合格",
                                         "初始数据库临时库-合格率%",
                                         "初始数据库正式库-上报数",
                                         "初始数据库正式库-上报进度",
                                         "合计-应报数",
                                         "合计-上报进度",
                                         "合计-合格",
                                         "合计-不合格",
                                         "合计-合格率%");
            } else {
                headList = Arrays.asList("城市",
                                         "区县",
                                         "上报机构",
                                         "初始数据正式库总量",
                                         "初始数据库临时库-应报数",
                                         "初始数据库临时库-上报进度%",
                                         "初始数据库临时库-合格",
                                         "初始数据库临时库-不合格",
                                         "初始数据库临时库-合格率%",
                                         "初始数据库正式库-上报数",
                                         "初始数据库正式库-上报进度",
                                         "合计-应报数",
                                         "合计-上报进度",
                                         "合计-合格",
                                         "合计-不合格",
                                         "合计-合格率%");
            }
            try {
                // 导出
                ExcelUtils.exportExcel(headList, dataList, "上报进度汇总导出", response);
            } catch (Exception e) {
            }
        }
    }
}
